package com.edusite

class MuroController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        if (!session.user)
	 redirect(uri:"/")


        redirect(action: "list", params: params)
    }

    def list = {
        if (!session.user)
	 redirect(uri:"/")

        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [muroInstanceList: Muro.list(params), muroInstanceTotal: Muro.count()]
    }
    def listProfe = {
        if (!session.user)
	 redirect(uri:"/")

        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [muroInstanceList: Muro.list(params), muroInstanceTotal: Muro.count()]
    }

    def create = {
        if (!session.user)
	 redirect(uri:"/")

        def muroInstance = new Muro()
        muroInstance.properties = params
        return [muroInstance: muroInstance]
    }
        def createProfe = {
        if (!session.user)
	 redirect(uri:"/")

        def muroInstance = new Muro()
        muroInstance.properties = params
        return [muroInstance: muroInstance]
    }

    def save = {
        if (!session.user)
	 redirect(uri:"/")

        def muroInstance = new Muro(params)
        muroInstance.fechaenv= new Date()
        if (muroInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'muro.label', default: 'Muro'), muroInstance.id])}"
           
           if (session.user.tipo == "profesor"){
            redirect(action: "showProfe", id: muroInstance.id)
           } else {
            redirect(action: "show", id: muroInstance.id)}
        }
        else {
            render(view: "create", model: [muroInstance: muroInstance])
        }
    }

    def show = {
        if (!session.user)
	 redirect(uri:"/")

        def muroInstance = Muro.get(params.id)
        if (!muroInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'muro.label', default: 'Muro'), params.id])}"
            redirect(action: "list")
        }
        else {
            [muroInstance: muroInstance]
        }
    }
    def showProfe = {
        if (!session.user)
	 redirect(uri:"/")

        def muroInstance = Muro.get(params.id)
        if (!muroInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'muro.label', default: 'Muro'), params.id])}"
            redirect(action: "list")
        }
        else {
            [muroInstance: muroInstance]
        }
    }

    def edit = {
        def muroInstance = Muro.get(params.id)
        if (session.user.usuario != muroInstance.enviadopor.usuario)
              redirect(uri:"/muro/list")
           
        if (!muroInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'muro.label', default: 'Muro'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [muroInstance: muroInstance]
        }
    }

    def update = {
        
        def muroInstance = Muro.get(params.id)
        
        if (muroInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (muroInstance.version > version) {
                    
                    muroInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'muro.label', default: 'Muro')] as Object[], "Another user has updated this Muro while you were editing")
                    render(view: "edit", model: [muroInstance: muroInstance])
                    return
                }
            }
            muroInstance.properties = params
            if (!muroInstance.hasErrors() && muroInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'muro.label', default: 'Muro'), muroInstance.id])}"
                redirect(action: "show", id: muroInstance.id)
            }
            else {
                render(view: "edit", model: [muroInstance: muroInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'muro.label', default: 'Muro'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        if (!session.user)
	 redirect(uri:"/")
	redirect(uri:"/home/indice")

    }
}
